/**
 * @file NCRTest.cpp
 *
 * This file implements the test for the PCR calculation.
 *
 * @date Sep 04, 2009
 * @author lfor
 */

#include "PCRTest.h"

#include "cute.h"

#include "PCR.h"

// Test functions

/**
 * @brief Test PCR32 to/from PCR64 conversion macros
 *
 * This test implements conversion comparison with fixed known values and full back and forth conversion comparison
 * (convert in one direction then in the other direction and compare).
 */
void PCRTest_PCR64ToPCR32Conversions()
{
	unsigned long long ExpectedPCR64 = 0;
	unsigned long ExpectedPCR32 = 0;

	unsigned long long ActualPCR64 = 0;
	unsigned long ActualPCR32 = 0;

	ExpectedPCR32 = 0;
	ExpectedPCR64 = 0;
	ActualPCR32 = PCR64_TO_PCR32(ExpectedPCR64);
	ActualPCR64 = PCR32_TO_PCR64(ExpectedPCR32);
	ASSERT_EQUALM("Unexpected low-range PCR64 to PCR32 conversion", ExpectedPCR32, ActualPCR32);
	ASSERT_EQUALM("Unexpected low-range PCR32 to PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR32 = 0xFFFFFFFF;
	ExpectedPCR64 = 0x257FFFFFDA8LL;
	ActualPCR32 = PCR64_TO_PCR32(ExpectedPCR64);
	ActualPCR64 = PCR32_TO_PCR64(ExpectedPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR64 to PCR32 conversion", ExpectedPCR32, ActualPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR32 to PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR32 = 0;
	ActualPCR64 = PCR32_TO_PCR64(ExpectedPCR32);
	ActualPCR32 = PCR64_TO_PCR32(ActualPCR64);
	ASSERT_EQUALM("Unexpected low-range PCR32-PCR64-PCR32 conversion", ExpectedPCR32, ActualPCR32);

	ExpectedPCR32 = 0xFFFFFFFF;
	ActualPCR64 = PCR32_TO_PCR64(ExpectedPCR32);
	ActualPCR32 = PCR64_TO_PCR32(ActualPCR64);
	ASSERT_EQUALM("Unexpected full-range PCR32-PCR64-PCR32 conversion", ExpectedPCR32, ActualPCR32);

	ExpectedPCR64 = 0x257FFFFFDA8LL;
	ActualPCR32 = PCR64_TO_PCR32(ExpectedPCR64);
	ActualPCR64 = PCR32_TO_PCR64(ActualPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR64-PCR3-PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR64 = 0;
	ActualPCR32 = PCR64_TO_PCR32(ExpectedPCR64);
	ActualPCR64 = PCR32_TO_PCR64(ActualPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR64-PCR3-PCR64 conversion", ExpectedPCR64, ActualPCR64);

}

/**
 * @brief Test PCR to/from PCR32 conversion macros
 *
 * This test implements conversion comparison with fixed known values and full back and forth conversion comparison
 * (convert in one direction then in the other direction and compare).
 */
void PCRTest_PCRToPCR32Conversions()
{
	unsigned long long ExpectedPCRBase = 0;
	unsigned long ExpectedPCR32 = 0;

	unsigned long long ActualPCRBase = 0;
	unsigned long ActualPCR32 = 0;

	ExpectedPCR32 = 0;
	ExpectedPCRBase = 0;
	ActualPCR32 = PCR_TO_PCR32(ExpectedPCRBase);
	ActualPCRBase = PCR32_TO_PCR_BASE(ExpectedPCR32);
	ASSERT_EQUALM("Unexpected low-range PCR32 to PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected low-range PCR to PCR32 conversion", ExpectedPCR32, ActualPCR32);

	ExpectedPCR32 = 0xFFFFFFFF;
	ExpectedPCRBase = 0x1FFFFFFFELL;
	ActualPCR32 = PCR_TO_PCR32(ExpectedPCRBase);
	ActualPCRBase = PCR32_TO_PCR_BASE(ExpectedPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR32 to PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected full-range PCR to PCR32 conversion", ExpectedPCR32, ActualPCR32);

	ExpectedPCRBase = 0;
	ActualPCR32 = PCR_TO_PCR32(ExpectedPCRBase);
	ActualPCRBase = PCR32_TO_PCR_BASE(ActualPCR32);
	ASSERT_EQUALM("Unexpected low-range PCR-PCR32-PCR conversion, base", ExpectedPCRBase, ActualPCRBase);

	ExpectedPCRBase = 0x1FFFFFFFELL;
	ActualPCR32 = PCR_TO_PCR32(ExpectedPCRBase);
	ActualPCRBase = PCR32_TO_PCR_BASE(ActualPCR32);
	ASSERT_EQUALM("Unexpected full-range PCR-PCR32-PCR conversion, base", ExpectedPCRBase, ActualPCRBase);

	ExpectedPCR32 = 0;
	ActualPCRBase = PCR32_TO_PCR_BASE(ExpectedPCR32);
	ActualPCR32 = PCR_TO_PCR32(ActualPCRBase);
	ASSERT_EQUALM("Unexpected low-range PCR32-PCR-PCR32 conversion", ExpectedPCR32, ActualPCR32);

	ExpectedPCR32 = 0xFFFFFFFF;
	ActualPCRBase = PCR32_TO_PCR_BASE(ExpectedPCR32);
	ActualPCR32 = PCR_TO_PCR32(ActualPCRBase);
	ASSERT_EQUALM("Unexpected full-range PCR32-PCR-PCR32 conversion", ExpectedPCR32, ActualPCR32);
}

/**
 * @brief Test PCR to/from PCR64 conversion macros
 *
 * This test implements conversion comparison with fixed known values and full back and forth conversion comparison
 * (convert in one direction then in the other direction and compare).
 */
void PCRTest_PCRToPCR64Conversions()
{
	unsigned long long ExpectedPCRBase = 0;
	unsigned short ExpectedPCRExt = 0;
	unsigned long long ExpectedPCR64 = 0;

	unsigned long long ActualPCRBase = 0;
	unsigned short ActualPCRExt = 0;
	unsigned long long ActualPCR64 = 0;

	ExpectedPCR64 = 0;
	ExpectedPCRBase = 0;
	ExpectedPCRExt = 0;
	ActualPCR64 = PCR_TO_PCR64(ExpectedPCRBase, ExpectedPCRExt);
	ActualPCRBase = PCR64_TO_PCR_BASE(ExpectedPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ExpectedPCR64);
	ASSERT_EQUALM("Unexpected low-range PCR64 to PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected low-range PCR64 to PCR conversion, ext", ExpectedPCRExt, ActualPCRExt);
	ASSERT_EQUALM("Unexpected low-range PCR to PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR64 = 0x257FFFFFFFFLL;
	ExpectedPCRBase = 0x1FFFFFFFFLL;
	ExpectedPCRExt = 299;
	ActualPCR64 = PCR_TO_PCR64(ExpectedPCRBase, ExpectedPCRExt);
	ActualPCRBase = PCR64_TO_PCR_BASE(ExpectedPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ExpectedPCR64);
	ASSERT_EQUALM("Unexpected full-range PCR64 to PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected full-range PCR64 to PCR conversion, ext", ExpectedPCRExt, ActualPCRExt);
	ASSERT_EQUALM("Unexpected full-range PCR to PCR64 conversion", ExpectedPCR64, ActualPCR64);


	ExpectedPCRBase = 0;
	ExpectedPCRExt = 0;
	ActualPCR64 = PCR_TO_PCR64(ExpectedPCRBase, ExpectedPCRExt);
	ActualPCRBase = PCR64_TO_PCR_BASE(ActualPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ActualPCR64);
	ASSERT_EQUALM("Unexpected low-range PCR-PCR64-PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected low-range PCR-PCR64-PCR conversion, ext", ExpectedPCRExt, ActualPCRExt);

	ExpectedPCRBase = 0x5A5A5A5A;
	ExpectedPCRExt = 150;
	ActualPCR64 = PCR_TO_PCR64(ExpectedPCRBase, ExpectedPCRExt);
	ActualPCRBase = PCR64_TO_PCR_BASE(ActualPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ActualPCR64);
	ASSERT_EQUALM("Unexpected mid-range PCR-PCR64-PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected mid-range PCR-PCR64-PCR conversion, ext", ExpectedPCRExt, ActualPCRExt);

	ExpectedPCRBase = 0x1FFFFFFFFLL;
	ExpectedPCRExt = 299;
	ActualPCR64 = PCR_TO_PCR64(ExpectedPCRBase, ExpectedPCRExt);
	ActualPCRBase = PCR64_TO_PCR_BASE(ActualPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ActualPCR64);
	ASSERT_EQUALM("Unexpected full-range PCR-PCR64-PCR conversion, base", ExpectedPCRBase, ActualPCRBase);
	ASSERT_EQUALM("Unexpected full-range PCR-PCR64-PCR conversion, ext", ExpectedPCRExt, ActualPCRExt);

	ExpectedPCR64 = 0;
	ActualPCRBase = PCR64_TO_PCR_BASE(ExpectedPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ExpectedPCR64);
	ActualPCR64 = PCR_TO_PCR64(ActualPCRBase, ActualPCRExt);
	ASSERT_EQUALM("Unexpected low-range PCR64-PCR-PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR64 = 0x5A5A5A5A;
	ActualPCRBase = PCR64_TO_PCR_BASE(ExpectedPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ExpectedPCR64);
	ActualPCR64 = PCR_TO_PCR64(ActualPCRBase, ActualPCRExt);
	ASSERT_EQUALM("Unexpected mid-range PCR64-PCR-PCR64 conversion", ExpectedPCR64, ActualPCR64);

	ExpectedPCR64 = 0x257FFFFFFFFLL;
	ActualPCRBase = PCR64_TO_PCR_BASE(ExpectedPCR64);
	ActualPCRExt = PCR64_TO_PCR_EXT(ExpectedPCR64);
	ActualPCR64 = PCR_TO_PCR64(ActualPCRBase, ActualPCRExt);
	ASSERT_EQUALM("Unexpected full-range PCR64-PCR-PCR64 conversion", ExpectedPCR64, ActualPCR64);

}

